<template>
  <div id="detail">
    <top-nav-bar class="detail-nav-bar" ref="navbar" @titleClick="topNavbarClick" />
    <scroll class="content" ref="scroll" :probe-type="3" @scroll="contentScroll">
      <detail-swiper :swiperImg="topImages" />
      <goods-info :goods="goods" />
      <shop-info :shop="shopData" />
      <goods-detail :goods-detail="detailInfo" @imageLoad="imageLoad" />
      <goods-params ref="params" :goods-params="goodsParams" />
      <comment ref="comment" :comment="commentList" />
      <!-- 推荐数据使用goodslist组件进行复用 -->
      <goods-list ref="recommend" :goods="recommendList" />
    </scroll>
    <bottom-navbar @addCart="goCart" />
    <back-top @click.native="backTop" v-show="isBacktopShow" />
  </div>
</template>

<script>
import topNavBar from "./childComp/top-nav-bar";
import detailSwiper from "./childComp/detailSwiper";
import goodsInfo from "./childComp/goods-info";
import shopInfo from "./childComp/shop-info";
import goodsDetail from "./childComp/goods-detail";
import goodsParams from "./childComp/goods-params";
import comment from "./childComp/comment";
import goodsList from "components/content/goods/goodsList";
import bottomNavbar from "./childComp/bottom-navbar";

import scroll from "components/content/scroll/scroll";

import { getDetail, getRecommend, goods, shop, params } from "service/detail";
import { goodsItemImgLoadMixin, backTopMixin } from "utils/mixin";

import { mapActions } from "vuex";
export default {
  name: "detail",
  data() {
    return {
      iid: null,
      topImages: [],
      goods: {},
      shopData: {},
      detailInfo: {}, // 穿着效果
      goodsParams: {},
      commentList: [],
      recommendList: [],
      scrollCorrelationY: [], // 存储点击标题滚动到对应的内容的Y值
      currentScrollIndex: 0
    };
  },
  components: {
    topNavBar,
    detailSwiper,
    goodsInfo,
    shopInfo,
    goodsDetail,
    goodsParams,
    comment,
    goodsList,
    bottomNavbar,

    scroll
  },
  mixins: [goodsItemImgLoadMixin, backTopMixin],
  created() {
    this.iid = this.$route.params.iid;

    // 请求详情页数据
    this.getDetailData(this.iid);

    // 请求推荐数据
    getRecommend().then(res => {
      this.recommendList = res.data.list;
    });
  },
  destroyed() {
    // 离开home组件时，取消事件总线goodsItemImgLoad的监听
    this.$bus.$off("goodsItemImgLoad", this.goodsItemImgLoadListener);
  },
  methods: {
    ...mapActions(["addCart"]),
    topNavbarClick(index) {
      // 点击顶部导航，跳转到对应的位置
      /**scrollCorrelationY 的获取：
       * 获取所有主题的offsetTop，push进scrollCorrelationY这个数组
       * 获取正确的offsetTop？
       * 1.created 不行，不能获取元素，DOM没有渲染
       * 2.mounted 不行，数据可能没有获取到
       * 3.在获取到数据后的回调中不行，DOM还没有渲染完
       * 4.$nextTick也不行，因为图片的高度没有计算在内
       * 5. 在图片加载完成后，获取的offsetTop才是正确的高度
       *
       */
      this.$refs.scroll.scrollTo(0, -this.scrollCorrelationY[index], 100);
    },
    imageLoad() {
      this.$refs.scroll.refresh();

      this.scrollCorrelationY = [];
      this.scrollCorrelationY.push(0);
      this.scrollCorrelationY.push(this.$refs.params.$el.offsetTop);
      this.scrollCorrelationY.push(this.$refs.comment.$el.offsetTop);
      this.scrollCorrelationY.push(this.$refs.recommend.$el.offsetTop);
      this.scrollCorrelationY.push(Number.MAX_VALUE); // 向数组中多增加一个数，用于滚动界面时显示对应的标题做判断
    },

    contentScroll(position) {
      /**监听滚动 实现对应的标题联动效果 */
      const positionY = -position.y;
      // 假设scrollCorrelationY = [ 0,1000,2000,3000,∞ ]
      // positionY 大于0 并且小于等于1000 时   $refs.navbar.currentIndex = 0
      // positionY 大于1000 并且小于等于2000 时    $refs.navbar.currentIndex = 1
      // positionY 大于2000 并且小于等于3000 时    $refs.navbar.currentIndex = 2
      // positionY 大于3000 到 无穷大时   $refs.navbar.currentIndex = 3
      let length = this.scrollCorrelationY.length;
      for (let i = 0; i < length; i++) {
        if (
          this.currentScrollIndex != i && // 防止判断执行多次
          (positionY > this.scrollCorrelationY[i] &&
            positionY <= this.scrollCorrelationY[i + 1])
        ) {
          this.currentScrollIndex = i;
          this.$refs.navbar.currentIndex = i;
        }
      }

      /**设置返回顶部的显示 */
      this.isBacktopShow = -position.y > 1500;
    },

    getDetailData(iid) {
      getDetail(iid).then(res => {
        console.log(res);
        this.topImages = res.result.itemInfo.topImages;
        this.goods = new goods(
          res.result.itemInfo,
          res.result.columns,
          res.result.shopInfo
        );
        this.shopData = new shop(res.result.shopInfo);
        this.detailInfo = res.result.detailInfo;
        this.goodsParams = new params(res.result.itemParams);
        this.commentList = res.result.rate.list;
      });
    },

    goCart() {
      const product = {};
      product.image = this.topImages[0];
      product.title = this.goods.title;
      product.desc = this.detailInfo.desc;
      product.lowNowPrice = this.goods.lowNowPrice;
      product.iid = this.iid;

      this.addCart(product).then(res => {
        this.$toast({ message: res });
      });
    }
  }
};
</script>

<style lang="less" scoped>
#detail {
  position: relative;
  z-index: 99;
  background: white;
  height: 100vh;
  .detail-nav-bar {
    position: relative;
    z-index: 99;
    background-color: #fff;
  }
  .content {
    height: calc(100% - 93px);
  }
}
</style>